package com.eascs.ticket.project.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.exception.ServiceException;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.project.dao.ProjectTicketTypeDao;
import com.eascs.ticket.project.entity.ProjectTicketType;

@Service
public class ProjectTicketTypeService extends BaseSevice {

	@Inject
	private ProjectTicketTypeDao projectTicketTypeDao;

	public List<Object[]> getTicketTypeByProjectId(String projectId) {
		return projectTicketTypeDao.getTicketTypeByProjectId(projectId);
	}

	public List<Object[]> getNotSelectTicketTypeByProjectId(String projectId) {
		return projectTicketTypeDao.getNotSelectTicketTypeByProjectId(projectId);
	}

	public List<Object[]> getTicketTypeDataSouceByProjectId(String projectId) {
		return this.projectTicketTypeDao.getTicketTypeDataSouceByProjectId(projectId);
	}

	public List<Object[]> getMainTicketTypeDataSouceByProjectId(String projectId) {
		return this.projectTicketTypeDao.getMainTicketTypeDataSouceByProjectId(projectId);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void removeTicketTypeById(String id) {
		ProjectTicketType projectTicketType = this.proxyDao.getById(ProjectTicketType.class, id);
		if (projectTicketTypeDao.countProjectTicketType(projectTicketType.getProjectId(), projectTicketType.getTicketTypeCode()) > 0) {
			throw new ServiceException("该类型在该项目中已经被使用，不能做删除处理！");
		}
		this.proxyDao.deleteByIdWithEntity(projectTicketType);
	}

	public void insertProjectTicketType(String projectId, String ticketTypeCode) {
		ProjectTicketType projectTicketType = new ProjectTicketType();
		EntityUtil.initInsertEntity(projectTicketType);
		projectTicketType.setProjectId(projectId);
		projectTicketType.setTicketTypeCode(ticketTypeCode);
		this.proxyDao.insert(projectTicketType);
	}

	public boolean hasProjectTicketType(String projectId, String ticketTypeCode) {
		return this.proxyDao.findInteger("select count(1) from project_ticket_type where project_id=? and ticket_type_code=?", projectId, ticketTypeCode) > 0;
	}

}
